home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / music4c.sit / Music4C Folder / Sources Folder / MacInterface2.c < prev    next >
Text File  |  1990-09-09  |  10KB  |  391 lines

  1. /*
  2. * ⌐ Graeme Gerrard 1990
  3. * Faculty of Music, University of Melbourne
  4. * Parkville Victoria 3052 Australia.
  5. *
  6. * ARPANET: grae@murdu.ucs.unimelb.edu.au
  7. * telephone: (613) 344 4127, Fax: (613) 344 5346
  8. */
  9.  
  10.  
  11. #include    "Music4C.h"
  12. #include    "Music4C_Prototype.h"
  13. #include    "Synthesis_Time.h"
  14.  
  15.  
  16. void            prepareSDfile(double);
  17. void            prepareAIFFfile(double, int);
  18. Boolean            pass3(void);
  19. void            FixHeaderInfo(void);
  20. void            tell_mem(void);
  21. Boolean            Mac_write3(float *, float);
  22. void            readBack(void);
  23. DialogPtr       thePass3DialogPtr;
  24.  
  25. extern void   D_Synthesis_Progress(void);
  26.  
  27. static pascal char  MyFilter (DialogPtr   theDialog,  EventRecord   *theEvent,  short  *itemHit);
  28. static void  Refresh_Dialog(DialogPtr    GetSelection);
  29. void    DisplayDialog(DialogPtr);
  30.  
  31.  
  32. #define  I_Cancel   1
  33. #define  I_OK   1
  34.  
  35. static void  Refresh_Dialog(GetSelection) 
  36.     DialogPtr    GetSelection;
  37.     Rect    tempRect;
  38.     short    DType;
  39.     Handle    DItem;
  40.  
  41.     GetDItem(GetSelection,I_OK, &DType, &DItem, &tempRect);
  42.     PenSize(3, 3);
  43.     InsetRect(&tempRect, -4, -4);
  44.     FrameRoundRect(&tempRect, 16, 16); 
  45.     PenSize(1, 1); 
  46.     
  47.  
  48. /* ======================================================== */
  49.  
  50. static        Handle        aHand;
  51. extern        OSErr        theErr;
  52. extern        Boolean        AIFFoddByte;
  53. extern        int            nrec;
  54. static        Rect        prgBox;
  55. extern        double        TotalDuration;
  56. extern        int            nchnls;
  57. extern        double        srate;
  58. static        double        barStep;
  59. static        int            NumberOfRecordsToWrite;
  60. static        int            *theIbuf;
  61. static        double        theScalefactor;
  62. static        DialogPtr   GetSelection;
  63. static        int                type;
  64.  
  65.  
  66. Boolean    MAC_do_pass3()
  67. {
  68.     Rect            tempRect;
  69.     short            itemHit;
  70.     long            nTicks;
  71.     long            startTick;
  72.     long            TotalTicks;
  73.     char            theChar;
  74.     extern            CursHandle    theCursor;
  75.     extern            ioParam        myIOParmBlk;
  76.     extern            int            SFoutputType;
  77.     extern            Boolean        CreateSoundFile;
  78.     extern            long        TotalSamps;
  79.     extern            Str255        theMess1;
  80.     Boolean            result;
  81.     
  82.      
  83.     GetSelection = GetNewDialog(PASS3_DIALOG, NIL, (WindowPtr)-1);
  84.     thePass3DialogPtr = GetSelection;
  85.     
  86.     tempRect.top = GetSelection->portRect.top;
  87.     tempRect.left = GetSelection->portRect.left;
  88.     tempRect.bottom = GetSelection->portRect.bottom;
  89.     tempRect.right = GetSelection->portRect.right;
  90.     tempRect.top = -GetSelection->portBits.bounds.top;
  91.     tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;
  92.     MoveWindow(GetSelection, tempRect.left, tempRect.top, TRUE);
  93.  
  94.     NumberOfRecordsToWrite = (int) (((TotalDuration * nchnls) * (srate / SAMPBUFSZ)) + 0.5);
  95.     GetDItem(GetSelection, 2, &type, &aHand, &prgBox);
  96.     barStep = (double)(prgBox.right - prgBox.left ) / (double)NumberOfRecordsToWrite;
  97.  
  98.     theCursor = GetCursor(watchCursor);
  99.     SetCursor(*theCursor);
  100.     
  101.     /* are we dealing with a SD file ? */
  102.     if ( CreateSoundFile ) {
  103.         if ( SFoutputType == SD ) {
  104.             myIOParmBlk.ioPosMode = fsAtMark;
  105.             myIOParmBlk.ioPosOffset = NIL;
  106.             myIOParmBlk.ioCompletion = NIL;
  107.             if ( nchnls == 1 )
  108.                 prepareSDfile(TotalDuration);
  109.             theScalefactor = SAMPMAX; /* not actually used */
  110.             theIbuf = (int *)NewPtr(sizeof(int) * SAMPBUFSZ);
  111.         }
  112.         else if ( SFoutputType == AIFF ) {
  113.             prepareAIFFfile(TotalDuration, nchnls);
  114.             theScalefactor = SAMPMAX; /* not actually used */
  115.             theIbuf = (int *)NewPtr(sizeof(int) * SAMPBUFSZ);
  116.         }
  117.         else if ( SFoutputType == INT16 ) {
  118.             theIbuf = (int *)NewPtr(sizeof(int) * SAMPBUFSZ);
  119.             theScalefactor = 1.0;
  120.             myIOParmBlk.ioReqCount =  TotalDuration * nchnls * srate * sizeof(int);
  121.             myIOParmBlk.ioCompletion = NIL;
  122.             if ( (theErr = PBAllocContig(&myIOParmBlk, FALSE)) != noErr ) {
  123.                 if ( theErr == dskFulErr ) {
  124.                     if ( (theErr = PBAllocate(&myIOParmBlk, FALSE)) != noErr ) {
  125.                         PstringCopy((char *)theMess1, "\pCan't allocate disk space for sound file");
  126.                         OSError(theMess1, NIL, NIL);
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.     }
  132.     
  133.     startTick = TickCount();
  134.     FlushEvents(everyEvent,0);
  135.     ShowWindow(GetSelection);
  136.     SelectWindow(GetSelection);
  137.     SetPort(GetSelection);
  138.     DisplayDialog(GetSelection);
  139.     result = pass3();
  140.     DisposDialog(GetSelection);
  141.     
  142.     /* all finished */
  143.     if ( SFoutputType == AIFF ) {
  144.         if ( AIFFoddByte ) {
  145.             myIOParmBlk.ioReqCount = 1L;
  146.             theChar = '\0';
  147.             myIOParmBlk.ioBuffer = (Ptr)theChar;
  148.             if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
  149.                 PstringCopy((char *)theMess1, "\pError writing to sample file");
  150.                 OSError(theMess1, NIL, NIL);
  151.             }
  152.         }
  153.         FixHeaderInfo();
  154.     }
  155.     TotalTicks = TickCount() - startTick;
  156.     NumToString((TotalTicks / 60), theMess1);
  157.     InitCursor();
  158.     ParamText(theMess1, NIL, NIL, NIL);
  159.     D_Synthesis_Time();
  160.     if ( result )
  161.         return(TRUE);
  162.     else
  163.         return(FALSE);
  164. }
  165.  
  166. Boolean    Mac_write3(theSamps, synthTime)
  167.     float    *theSamps;
  168.     float    synthTime;
  169. {
  170.     register        i;
  171.     extern            Boolean        CreateSoundFile;
  172.     extern            int            wrote_rec;
  173.     extern            ioParam        myIOParmBlk;
  174.     long            nBytes;
  175.     EventRecord        event;
  176.     long            Number;
  177.     long            aLong;
  178.     char            c;
  179.     Rect            aRect;
  180.     int                type;
  181.     int                rightPoint;
  182.     int                *Iptr;
  183.     float            *sp;
  184.     double            x;
  185.     long            nSamps;
  186.     Point            where, P1, P2, P3;
  187.     Rect            tempRect;
  188.     short            DType;
  189.     short            itemHit;
  190.     Handle            DItem;
  191.     extern            float        MaxSample;
  192.     extern            float        MinSample;
  193.     long    fTicks;
  194.     
  195.     nrec++;
  196.     nBytes = (long)SAMPBUFSZ * sizeof(float);
  197.  
  198.     if (CreateSoundFile) {
  199.         myIOParmBlk.ioCompletion = NIL;
  200.         switch(SFoutputType ) {
  201.             case    FLOAT:
  202.                 myIOParmBlk.ioReqCount = nBytes;
  203.                 myIOParmBlk.ioBuffer = (Ptr)theSamps;
  204.                 if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) { 
  205.                     PstringCopy((char *)theMess1, "\pError writing to sample file");
  206.                     OSError(theMess1, NIL, NIL);
  207.                 }
  208.                 if ( myIOParmBlk.ioActCount != nBytes ) {
  209.                     PstringCopy((char *)theMess1, "\pError writing to sample file wrote wrong number of bytes");
  210.                     OSError(theMess1, NIL, NIL);
  211.                 }                
  212.                 break;
  213.             case    SD:
  214.             case    SD2:
  215.                 nSamps = SAMPBUFSZ;
  216.                 nBytes = (long)SAMPBUFSZ * sizeof(int);
  217.                 for ( i = 0, Iptr = theIbuf, sp = theSamps; i < nSamps; i++ ) {
  218.                     x = *sp++;
  219.                     if ( x < MinSample )
  220.                         MinSample = x;
  221.                     if ( x > MaxSample )
  222.                         MaxSample = x;
  223.                     *Iptr++ = (int)x;
  224.                 }
  225.                 /* write it out */
  226.                 myIOParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  227.                 myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  228.                 if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) { 
  229.                     PstringCopy((char *)theMess1, "\pError writing to sample file");
  230.                     OSError(theMess1, NIL, NIL);
  231.                 }
  232.                 if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount ) {
  233.                     PstringCopy((char *)theMess1, "\pError writing to sample file wrote wrong number of bytes");
  234.                     OSError(theMess1, NIL, NIL);
  235.                 }
  236.                 break;
  237.             case    AIFF:
  238.                 nSamps = SAMPBUFSZ;
  239.                 TotalSamps += nSamps;
  240.                 nBytes = (long)SAMPBUFSZ * sizeof(int);
  241.                 for ( i = 0, Iptr = theIbuf, sp = theSamps; i < nSamps; i++ ) {
  242.                     x = *sp++;
  243.                     if ( x < MinSample )
  244.                         MinSample = x;
  245.                     if ( x > MaxSample )
  246.                         MaxSample = x;
  247.                     *Iptr++ = (int)x;
  248.                 }
  249.                 /* write it out */
  250.                 myIOParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  251.                 myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  252.                 if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) { 
  253.                     PstringCopy((char *)theMess1, "\pError writing to sample file");
  254.                     OSError(theMess1, NIL, NIL);
  255.                 }
  256.                 if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount ) {
  257.                     PstringCopy((char *)theMess1, "\pError writing to sample file wrote wrong number of bytes");
  258.                     OSError(theMess1, NIL, NIL);
  259.                 }
  260.                 break;
  261.             case    INT16:
  262.                 nSamps = SAMPBUFSZ;
  263.                 nBytes = (long)SAMPBUFSZ * sizeof(int);
  264.                 for ( i = 0, Iptr = theIbuf, sp = theSamps; i < nSamps; i++ ) {
  265.                     x = *sp++;
  266.                     if ( x < MinSample )
  267.                         MinSample = x;
  268.                     if ( x > MaxSample )
  269.                         MaxSample = x;
  270.                     *Iptr++ = (int)x;
  271.                 }
  272.                 /* write it out */
  273.                 myIOParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  274.                 myIOParmBlk.ioBuffer = (Ptr)theIbuf;
  275.                 if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) { 
  276.                     PstringCopy((char *)theMess1, "\pError writing to sample file");
  277.                     OSError(theMess1, NIL, NIL);
  278.                 }
  279.                 if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount ) {
  280.                     PstringCopy((char *)theMess1, "\pError writing to sample file wrote wrong number of bytes");
  281.                     OSError(theMess1, NIL, NIL);
  282.                 }
  283.                 break;
  284.         }
  285.     }
  286.     SetPort(GetSelection);
  287.     rightPoint = prgBox.left + (nrec * barStep);
  288.     if ( rightPoint > prgBox.right )
  289.         rightPoint = prgBox.right;
  290.     if ( nrec == NumberOfRecordsToWrite )
  291.         rightPoint = prgBox.right;
  292.         
  293.     SetRect(&aRect, prgBox.left, prgBox.top, rightPoint, prgBox.bottom);
  294.     FillRect(&aRect, gray);
  295. /*    Refresh_Dialog(GetSelection); */
  296.     
  297.     wrote_rec = 1;
  298.     GetNextEvent(everyEvent, &event);
  299.     if ( IsDialogEvent(&event) ) {
  300.         if ( DialogSelect(&event, &GetSelection, &itemHit)) {
  301.             if ( itemHit == 1) {
  302.                 SetRect(&tempRect, 185, 72, 265, 92 );
  303.                 InsetRect(&tempRect, 1, 1 );
  304.                 InvertRoundRect(&tempRect, 8, 8 );
  305.                 Delay(30L, &fTicks);
  306.                 return(FALSE);
  307.             }
  308.         }
  309.     }
  310.     return(TRUE);
  311. }
  312.  
  313.  
  314. void    tell_mem()
  315. {
  316.     long    freemem1;
  317.     long    stackspace1;
  318.     Size    maxmem1;
  319.     long    maxblock1;
  320.     long    aLong1, anotherLong1;
  321.     Size    aSize1;
  322.     THz        syszone1;
  323.     Size    totalmem1;
  324.     
  325.     long    freemem2;
  326.     long    stackspace2;
  327.     Size    maxmem2;
  328.     long    maxblock2;
  329.     long    aLong2, anotherLong2;
  330.     Size    aSize2;
  331.     THz        syszone2;
  332.     Size    totalmem2;
  333.     
  334.     
  335.     freemem1 = FreeMem();
  336.     maxblock1 = MaxBlock();
  337.     maxmem1 =  (long)MaxMem(&aSize1);
  338.     stackspace1 = StackSpace();
  339.     syszone1 = SystemZone();
  340.     anotherLong1 = (long)syszone1;
  341.     
  342.     totalmem1 = (long)CompactMem(maxSize);
  343.  
  344.     freemem2 = FreeMem();
  345.     maxblock2 = MaxBlock();
  346.     maxmem2 =  (long)MaxMem(&aSize2);
  347.     stackspace2 = StackSpace();
  348.     syszone2 = SystemZone();
  349.     anotherLong2 = (long)syszone2;
  350.     totalmem2 = (long)CompactMem(maxSize);
  351. }
  352.  
  353.  
  354.  
  355. void    readBack()
  356. {
  357.     extern    float    *SampsPtr;
  358.     long    nBytes;
  359. /* this code reads it back for checking    */            
  360.     myIOParmBlk.ioPosMode = fsFromMark;
  361.     myIOParmBlk.ioPosMode = fsFromStart;
  362.     myIOParmBlk.ioPosOffset = NIL;
  363.     theErr = PBSetFPos(&myIOParmBlk, FALSE);
  364.     myIOParmBlk.ioPosMode = fsAtMark;
  365.  
  366.     myIOParmBlk.ioReqCount = nBytes;
  367.     myIOParmBlk.ioPosMode = fsAtMark;
  368.     myIOParmBlk.ioPosOffset = NIL;
  369.     myIOParmBlk.ioBuffer = (Ptr)SampsPtr;
  370.     if ( (theErr = PBRead(&myIOParmBlk, FALSE)) != noErr ) { 
  371.             PstringCopy((char *)theMess1, "\pError reading back sample file");
  372.             OSError(theMess1, NIL, NIL);
  373.     }
  374.     if ( myIOParmBlk.ioActCount != nBytes ) {
  375.             PstringCopy((char *)theMess1, "\pError reading back sample file wrote wrong number of bytes");
  376.             OSError(theMess1, NIL, NIL);
  377.     }
  378.     
  379. }
  380.  
  381.  
  382. void    DisplayDialog(GetSelection)
  383.     DialogPtr    GetSelection;
  384. {
  385.     DrawDialog(GetSelection);
  386.     GetDItem(GetSelection, 2, &type, &aHand, &prgBox);
  387.     FrameRect(&prgBox);
  388. }
  389.